package yxy.game.pm2.module.yiling;

import org.joda.time.DateTime;
import org.joda.time.LocalDate;
import yxy.apple.util.redisson.RedisException;
import yxy.game.pm2.bean.db.dao.MySQLCacheDAO;
import yxy.game.pm2.bean.module.PlayerModule;
import yxy.game.pm2.bean.module.RedisLock;
import yxy.game.pm2.module.tower.TowerModule;

import java.util.ArrayList;
import java.util.concurrent.TimeUnit;

public class YiLingBattleModule extends PlayerModule {
    // bean===========================================================================

    public static int sweepCost = 50;
    //缓存过期时间
    private static long expire = 1;
    private static TimeUnit expireTimeUnit = TimeUnit.DAYS;

    private int challengeMaxtimes = 3; // 初始挑战次数
    private int buyMaxtimes = 3; // 初始购买次数

    private int mapId;

    private int times;// 今天的挑战次数

    private int buytimes;// 今天的购买次数

    private int maxCustomsPass; //每天最大挑战关卡数

    private DateTime time;// 时间标记

    private ArrayList<Integer> levelReward = new ArrayList<>();// 宝箱已领取奖励

    private ArrayList<Integer> goThroughReward = new ArrayList<>();// 通关奖励已领取的奖励


    public int getBuytimes() {
        return buytimes;
    }

    public void setBuytimes(int buytimes) {
        this.buytimes = buytimes;
    }


    public static long getExpire() {
        return expire;
    }

    public static void setExpire(long expire) {
        YiLingBattleModule.expire = expire;
    }

    public static TimeUnit getExpireTimeUnit() {
        return expireTimeUnit;
    }

    public static void setExpireTimeUnit(TimeUnit expireTimeUnit) {
        YiLingBattleModule.expireTimeUnit = expireTimeUnit;
    }

    public int getTimes() {
        return times;
    }

    public void setTimes(int times) {
        this.times = times;
    }

    public DateTime getTime() {
        return time;
    }

    public void setTime(DateTime time) {
        this.time = time;
    }

    public int getMapId() {
        return mapId;
    }

    public void setMapId(int mapId) {
        this.mapId = mapId;
    }

    public ArrayList<Integer> getLevelReward() {
        return levelReward;
    }

    public void setLevelReward(ArrayList<Integer> levelReward) {
        this.levelReward = levelReward;
    }

    public int getMaxCustomsPass() { return maxCustomsPass; }

    public void setMaxCustomsPass(int maxCustomsPass) { this.maxCustomsPass = maxCustomsPass; }

    public ArrayList<Integer> getGoThroughReward() {
        return goThroughReward;
    }

    public void setGoThroughReward(ArrayList<Integer> goThroughReward) {
        this.goThroughReward = goThroughReward;
    }

    // func===========================================================================

    public void checkLevelReward() {
    }

    // redis==========================================================================
    private static String key(String playerCode) {
        return String.format("PLAYER:%s:MODULE:YILINGBATTLE", playerCode);
    }

    /**
     * 扫荡次数刷新
     */
    private void update() {
        DateTime now = DateTime.now();
        if (getTime() == null || !new LocalDate(now).equals(new LocalDate(getTime()))) {
            try {
                if (tryLock()) {
                    //第二天就重置挑战次数和购买挑战次数
                    setTime(now);
                    setTimes(0);
                    setBuytimes(0);
                    setMaxCustomsPass(0);
                    this.set();
                }
            } catch (Exception e) {
            } finally {
                unlock();
            }
        }
    }


    //剩余挑战次数 = 初始的三次 + 购买的次数 - 已经挑战的次数
    public int remainTimes() {
        int remain = getBuytimes() + challengeMaxtimes - getTimes();
        return remain;
    }

    //剩余购买扫荡次数 =  初始的三次 - 购买的次数
    public int remainBuyTimes() {
        int remain = buyMaxtimes - getBuytimes();
        return remain;
    }


    public void set() {
        try {
            String key1 = key(getPlayerCode());
            setv(key1, this);
//            check();//红点
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    static public YiLingBattleModule get(String playerCode) {
        try {
            String key = key(playerCode);
            YiLingBattleModule module = getv(key);
            //todo 加到数据库
            if (module == null) {
                module = MySQLCacheDAO.getV(key, YiLingBattleModule.class);
                if (module != null) module.set();
            }

            if (module == null) {
                module = create(playerCode);
            }
            module.update();
            return module;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    //第一次初始化
    static private YiLingBattleModule create(String playerCode) {
        DateTime now = DateTime.now();
        YiLingBattleModule module = new YiLingBattleModule();
        module.setPlayerCode(playerCode);
        module.setMapId(1);
        module.setTime(now);
        module.setTimes(0);
        module.setBuytimes(0);
        module.set();
        return module;
    }

    //存储到数据库的方法
    public static void saveToDB(String playerCode) {
        try {
            String key = key(playerCode);
            if (touchBucket(key).isExists()) {
                YiLingBattleModule yiLingBattleModule = YiLingBattleModule.get(playerCode);
                MySQLCacheDAO.setV(key, yiLingBattleModule);
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    //从redis移走数据的操作
    public static void removeAllFromRedis(String playerCode) {
        try {
            touchBucket(key(playerCode)).delete();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    // lock==========================================================================

    @Override
    public String lockKey() {
        return String.format("lock:%s", key(getPlayerCode()));
    }

    public static RedisLock lock(String playerCode) {
        return getLock(String.format("lock:%s", key(playerCode)));
    }

}
